package com.j.sdks.web;

import org.apache.commons.lang.RandomStringUtils;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

public class BaseController<T> {
	protected String basePath;
	protected Log log = LogFactory.getLog(getClass());

	public BaseController(String basePath) {
		super();
		this.basePath = basePath;
	}

	@RequestMapping(value = "/", method = RequestMethod.GET)
	public String execute() {
		log.info("执行默认请求,转发到" + basePath + "index");
		return forward("index");
	}

	/**
	 * 
	 * @param path
	 *            视图位置路径(请结合[rootServlet-context.xml] =
	 *            org.springframework.web.servlet
	 *            .view.InternalResourceViewResolver 节点)
	 * @example /views/view ==>> view表示view.jsp
	 * @return
	 */
	protected String forwardView(String path) {
		return new StringBuffer(basePath).append(path).toString();
	}

	/**
	 * 
	 * @param path
	 *            视图位置路径(请结合[rootServlet-context.xml] =
	 *            org.springframework.web.servlet
	 *            .view.InternalResourceViewResolver 节点)
	 * @param args
	 *            [key=value,...];[id=123,name=jack] 参数
	 * @example /views/view ==>> view表示view.jsp
	 * @return
	 */
	protected String forwardView(String path, String... args) {
		StringBuffer buffer = new StringBuffer(basePath);
		buffer.append(path);
		if (args != null && args.length > 0) {
			buffer.append("?");
			for (int i = 0; i < args.length; i++) {
				buffer.append(args[i]);
				if (i < args.length - 1) {
					buffer.append("&");
				}
			}
		}
		return buffer.toString();
	}

	/**
	 * 
	 * @param path
	 *            视图衍射（访问）路径
	 * @return /views/view ==>>view 被映射的路径
	 */
	protected String forward(String path) {
		return new StringBuffer("forward:").append(path).toString();
	}

	/**
	 * 
	 * @param path
	 *            视图衍射（访问）路径
	 * @param args
	 *            [key=value,...];[id=123,name="tom"]
	 * @return /views/view ==>>view 被映射的路径
	 */
	protected String forward(String path, String... args) {
		StringBuffer buffer = new StringBuffer("forward:");
		buffer.append(path);
		if (args != null && args.length > 0) {
			buffer.append("?");
			for (int i = 0; i < args.length; i++) {
				buffer.append(args[i]);
				if (i < args.length - 1) {
					buffer.append("&");
				}
			}
		}
		return buffer.toString();
	}

	/**
	 * 
	 * @param path
	 *            视图衍射（访问）路径
	 * @example /views/input.html
	 * @return
	 */
	protected String redirect(String path) {
		return new StringBuffer("redirect:").append(path).toString();
	}

	/**
	 * 
	 * @param path
	 *            path 视图衍射（访问）路径
	 * @param args
	 * @return
	 */
	protected String redirect(String path, String... args) {
		StringBuffer buffer = new StringBuffer("redirect:");
		buffer.append(path);
		if (args != null && args.length > 0) {
			buffer.append("?");
			for (int i = 0; i < args.length; i++) {
				buffer.append(args[i]);
				if (i < args.length - 1) {
					buffer.append("&");
				}
			}
		}
		return buffer.toString();
	}

	public static void main(String[] args) {
		String ts = "wp_commentmeta,wp_comments,wp_links,wp_options,wp_postmeta,wp_posts,wp_term_relationships,wp_term_taxonomy,wp_terms,wp_usermeta,wp_users";
		String[] tss = ts.split(",");
		for (int i = 0; i < tss.length; i++) {
			System.out.println("<table schema=\"wordpress\" tableName=\""+tss[i]+"\"></table>");
		}
		
		System.out.println(RandomStringUtils.randomAlphanumeric(8)
				.toLowerCase());
	}
}
